﻿using Framework.Utility.Helper;
using Framework.Utility.ViewModels;
using Microsoft.Extensions.Logging;
using MonitorCode.Hangfire;
using System.Data;

namespace MonitorCode.DBCode
{
    public class SqlDBService : IDBService
    { 
        private readonly string _connStr;
        private readonly SqlHelper _helper;

        public SqlDBService(DBLinkModel model)
        { 
            _connStr = $"Data Source ={model.DBHostName};Initial Catalog = {model.DatabaseName};User id = {model.UserName};Password = {model.Password}";
            _helper = new SqlHelper(_connStr);

        }
        public TData ExecuteNonQuery(string sql)
        {
            var count = _helper.ExecuteCommand(sql);
            return new TData(ResultTag.success, "", count);
        }
        public TData GetCountBySql(string sql)
        { 
            var dt = _helper.GetDataTable(sql);
            return new TData(ResultTag.success, "", dt.Rows.Count);
        }

        public TData GetListBySql<T>(string sql)
        { 
            var dt = _helper.GetDataTable(sql);
            var list = dt.ConvertDataTableToList<T>();
            TData data = new TData(ResultTag.success);
            data.data = list;
            data.total = list.Count;
            return data;
        }


        public List<T> GetDataListBySql<T>(string sql)
        {
            var dt = _helper.GetDataTable(sql);
            return dt.ConvertDataTableToList<T>();
        }
    }
}